import axios from 'axios';
import variable from '../../constants/variable';
import {htmlFilter} from '../../common';

// Actions
const DUBBO_FETCH_DUBBO_PROJECT_LIST = 'yapi/dubboproject/FETCH_DUBBO_PROJECT_LIST';
const DUBBO_PROJECT_ADD = 'yapi/dubboproject/PROJECT_ADD';
const DUBBO_PROJECT_DEL = 'yapi/dubboproject/PROJECT_DEL';
const DUBBO_PROJECT_UPDATE = 'yapi/dubboproject/PROJECT_UPDATE';
const DUBBO_PROJECT_UPDATE_ENV = 'yapi/dubboproject/PROJECT_UPDATE_ENV';
const DUBBO_PROJECT_ADD_ENV = 'yapi/dubboproject/PROJECT_ADD_ENV';
const DUBBO_PROJECT_UPSET = 'yapi/dubboproject/PROJECT_UPSET';
const DUBBO_GET_CURR_PROJECT = 'yapi/dubboproject/GET_CURR_PROJECT';
const DUBBO_GET_PEOJECT_MEMBER = 'yapi/dubboproject/GET_PEOJECT_MEMBER';
const DUBBO_ADD_PROJECT_MEMBER = 'yapi/dubboproject/ADD_PROJECT_MEMBER';
const DUBBO_DEL_PROJECT_MEMBER = 'yapi/dubboproject/DEL_PROJECT_MEMBER';
const DUBBO_CHANGE_PROJECT_MEMBER = 'yapi/dubboproject/CHANGE_PROJECT_MEMBER';
const DUBBO_CHECK_PROJECT_NAME = 'yapi/dubboproject/CHECK_PROJECT_NAME';
const DUBBO_COPY_PROJECT_MSG = 'yapi/dubboproject/COPY_PROJECT_MSG';
const DUBBO_PROJECT_GET_ENV = 'yapi/dubboproject/PROJECT_GET_ENV';
const DUBBO_CHANGE_MEMBER_EMAIL_NOTICE = 'yapi/dubboproject/CHANGE_MEMBER_EMAIL_NOTICE';
const DUBBO_GET_SWAGGER_URL_DATA = 'yapi/dubboproject/GET_SWAGGER_URL_DATA';
// 20190904 11:09 wms
const DUBBO_GET_TOKEN = 'yapi/dubboproject/GET_TOKEN';
// Reducer
const initialState = {
    memberList:[], // ttc add 暂时没用
    dubboProjectList:[],
    currProject: {},
    isUpdateModalShow: false,
    handleUpdateIndex: -1,
    projectMsg: {},
    userInfo: {},
    tableLoading: true,
    total: 0,
    currPage: 1,
    token: '',
    projectEnv: {
        env: [
            {
                header: []
            }
        ]
    },
    swaggerUrlData: ''
};

export default (state = initialState, action) => {
  switch (action.type) {
    case DUBBO_GET_CURR_PROJECT: {
        return {
            ...state,
            currProject: action.payload.data.data
        };
    }

    case DUBBO_GET_TOKEN: {
       return {
            ...state,
           token: action.payload.data.data
       };
    }
    case DUBBO_FETCH_DUBBO_PROJECT_LIST: {
      return {
        ...state,
        dubboProjectList: action.payload.data.data.list
      };
    }
    case DUBBO_GET_PEOJECT_MEMBER: {
      return {
        ...state,
        memberList: action.payload.data.data
      };
    }
    default:
      return state;
  }
};

// 获取某分组下的项目列表
export function fetchDubboProjectList(id, pageNum) {
  return {
    type: DUBBO_FETCH_DUBBO_PROJECT_LIST,
    payload: axios.get('/api/dubboproject/list', {
      params: {
        group_id: id,
        page: pageNum || 1,
        limit: variable.PAGE_LIMIT
      }
    })
  };
}

// 复制项目
export function copyProjectMsg(params) {
  return {
    type: DUBBO_COPY_PROJECT_MSG,
    payload: axios.post('/api/dubboproject/copy', params)
  };
}

// 添加项目成员
export function addMember(param) {
  return {
    type: DUBBO_ADD_PROJECT_MEMBER,
    payload: axios.post('/api/dubboproject/add_member', param)
  };
}

// 删除项目成员
export function delMember(param) {
  return {
    type: DUBBO_DEL_PROJECT_MEMBER,
    payload: axios.post('/api/dubboproject/del_member', param)
  };
}

// 修改项目成员权限
export function changeMemberRole(param) {
  return {
    type: DUBBO_CHANGE_PROJECT_MEMBER,
    payload: axios.post('/api/dubboproject/change_member_role', param)
  };
}
// 修改项目成员是否收到邮件通知
export function changeMemberEmailNotice(param) {
  return {
    type: DUBBO_CHANGE_MEMBER_EMAIL_NOTICE,
    payload: axios.post('/api/dubboproject/change_member_email_notice', param)
  };
}

// 获取项目成员列表
export function getProjectMemberList(id) {
  return {
    type: DUBBO_GET_PEOJECT_MEMBER,
    payload: axios.get('/api/dubboproject/get_member_list', {
      params: { id }
    })
  };
}


export function addProject(data) {
  let {
    cnname,
    enname,
    dgroup,
    desc,
    group_id,
    icon,
    color,
    onlinedate,
    expname,
    childbase
  } = data;
  // 过滤项目名称中有html标签存在的情况
  cnname = htmlFilter(cnname);
  const param = {
   cnname,
   enname,
   dgroup,
   desc,
   group_id,
   icon,
   color,
   onlinedate,
    expname,
    childbase
  };
  console.log('----------------------')
  console.log(param)
  return {
    type: DUBBO_PROJECT_ADD,
    payload: axios.post('/api/dubboproject/add', param)
  };
}

// 修改项目
export function updateProject(data) {
  let {
      id,
      cnname,
      enname,
      dgroup,
      desc,
      groupid,
      icon,
      color,
      onlinedate,
    expname,
    childbase
    } = data;

  const param = {
      id,
      cnname,
      enname,
      dgroup,
      desc,
      groupid,
      icon,
      color,
      onlinedate,
    expname,
    childbase
  };
  return {
    type: DUBBO_PROJECT_UPDATE,
    payload: axios.post('/api/dubboproject/up', param)
  };
}

// 修改项目脚本
export function updateProjectScript(data) {
  return {
    type: DUBBO_PROJECT_UPDATE,
    payload: axios.post('/api/dubboproject/up', data)
  };
}

// 修改全局mock
export function updateProjectMock(data) {
  return {
    type: DUBBO_PROJECT_UPDATE,
    payload: axios.post('/api/dubboproject/up', data)
  };
}

// 修改项目环境配置
export function updateEnv(data) {
  const { evnconfig, _id } = data;
  const param = {
    id: _id,
    evnconfig
  };
  return {
    type: DUBBO_PROJECT_UPDATE_ENV,
    payload: axios.post('/api/dubboproject/up_env', param)
  };
}

// 添加项目环境配置
export function addEnv(data) {
    const { evnconfig, _id } = data;
    const param = {
        id: _id,
        evnconfig
    };
    return {
        type: DUBBO_PROJECT_ADD_ENV,
        payload: axios.post('/api/dubboproject/add_env', param)
    };
}

// 获取项目环境配置
export function getEnv(project_id) {
  return {
    type: DUBBO_PROJECT_GET_ENV,
    payload: axios.get('/api/dubboproject/get_env', { params: { project_id } })
  };
}

// 修改项目头像
export function upsetProject(param) {
  return {
    type: DUBBO_PROJECT_UPSET,
    payload: axios.post('/api/dubboproject/upset', param)
  };
}

// 删除项目
export function delProject(id) {
  const param = { id };
  return {
    type: DUBBO_PROJECT_DEL,
    payload: axios.post('/api/dubboproject/del', param)
  };
}

export async function getProject(id) {
  let result = await axios.get('/api/dubboproject/get?id=' + id);
  return {
    type: DUBBO_GET_CURR_PROJECT,
    payload: result
  };
}

export async function checkProjectName(name, group_id) {
  return {
    type: DUBBO_CHECK_PROJECT_NAME,
    payload: axios.get('/api/dubboproject/check_project_name', {
      params: { name, group_id }
    })
  };
}

export async function handleSwaggerUrlData(url) {
    return {
        type: DUBBO_GET_SWAGGER_URL_DATA,
        payload: axios.get('/api/dubboproject/swagger_url?url='+url)
    };
}

// 20190904 11:12 wms
export async function getToken(project_id) {
    return {
        type: DUBBO_GET_TOKEN,
        payload: axios.get('/api/dubboproject/token', {
            params: { project_id }
        })
    };
}
